package com.it.you.common.utils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.RateLimiter;

/**
 * @Author : YouHui
 * @create 2023/5/29 15:57
 */
public class RateLimiterUtil {
    private static LoadingCache<String, RateLimiter> cache = CacheBuilder.newBuilder()
            .maximumSize(1000)
            .build(new CacheLoader<String, RateLimiter>() {
                @Override
                public RateLimiter load(String key) {
                    return RateLimiter.create(10); // 限制每秒10次请求
                }
            });

    public static RateLimiter getRateLimiter(String key) {
        return cache.getUnchecked(key);
    }
}
